//
// Created by 86156 on 25-7-12.
//

#include "pt505_driver.h"

#include <stdint.h>

#include "adc.h"
#include "cmsis_os2.h"
#include "control_driver.h"
#include "stm32f4xx_hal_adc.h"

int value=0;
float voltage=0.0f;
float voltage_last=4.548f;
float voltage_origin = 0.0f;
float voltage_treated = 0.0f;

float alpha =0.04f;
float DepthMeasure =0;
uint32_t adc_buffer;
float sum=0;

extern osSemaphoreId_t Pt505SemBinaryHandle;

void pt505_Init() {
    HAL_ADC_Start(&hadc1);
    voltage=HAL_ADC_GetValue(&hadc1);
    voltage_origin = voltage;
}

void pt505_Cal() {
    voltage=(1-alpha) * voltage_last + alpha * voltage* 33.f/4095.f;
    voltage_last=voltage;
    DepthMeasure = voltage*100;//300.0f*(voltage-4.54f); ///转化为厘米
}

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) {
    if(hadc==&hadc1)
    {
        osSemaphoreRelease(Pt505SemBinaryHandle);
    }

}